#!/usr/bin/env python
# encoding: utf-8


from typing import Any
from django import forms
from django.core.exceptions import ValidationError
from app01.forms.bootstrap import BootStrapForm
from app01 import models
from app01.forms.widgets import ColorRadioSelect
from django.contrib.auth.models import User,Group


class ProjectModelForm(BootStrapForm,forms.ModelForm):
	# desc = forms.CharField(widget=forms.Textarea)
	# bootstrap_class_exclude = ['color']

	class Meta:
		model = models.Project
		fields = ['name', 'color', 'desc']
		widgets = {
			'desc': forms.Textarea,
			'color': ColorRadioSelect(attrs={'class': 'color-radio'}),
			
		}

	# 重写初始化函数来获取视图函数中的传值
	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.request = request

	def clean_name(self):
		"""项目校验"""
		name = self.cleaned_data.get('name')  #self.cleaned_data 是 Django 表单验证后生成的一个字典，包含所有已通过验证的字段及其对应的值
		# 1. 当前用户是否已创建过此项目

		# 1.1 十一日是否已创建过此项目。
		exists = models.Project.objects.filter(name=name, creator=self.request.tracer.user).exists()
		if exists:
			raise ValidationError('项目名已存在')
	
		return name

class projectUserModelForm(BootStrapForm, forms.ModelForm):
	class Meta:
		model = models.ProjectUser
		fields = ['user']
	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)
		# 数据库找到所有的用户
		user_list_object = User.objects.all().values_list('id', 'username')
		self.fields['user'].choices =  list(user_list_object)

class projectStationform(BootStrapForm, forms.ModelForm):
	class Meta:
		model = models.ProjectStation
		fields = ['sequence']
	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)



class StationModelForm(BootStrapForm, forms.ModelForm):
	class Meta:
		model = models.Station
		fields =['name','catogory','equip_no']
	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)


class ProductModelForm(BootStrapForm, forms.ModelForm):
	class Meta:
		model = models.Product
		fields = ['name']
	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)


class WikiModelForm(BootStrapForm, forms.ModelForm):

	class Meta:
		model = models.Wiki
		fields = ['product','title', 'priority', 'deal_time','DRI' ]
	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.request = request

class FPY_rateModelForm(BootStrapForm, forms.ModelForm):

	class Meta:
		model = models.FPY_rate
		fields = ['spi_1','post_aoi_1','xray_1','TOP_analysis1','cause_solution1','spi_2','post_aoi_2','xray_2','TOP_analysis2','cause_solution2','product','shift','report_date']

	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.request = request
	def clean_report_date(self):
		"""项目校验"""
		report_date = self.cleaned_data.get('report_date')  #self.cleaned_data 是 Django 表单验证后生成的一个字典，包含所有已通过验证的字段及其对应的值
		product=self.cleaned_data.get('product')
		shift=self.cleaned_data.get('shift')
		# 1. 当前机种，当前线体，当天当班次 是否已创建良率报表
 
		exists = models.FPY_rate.objects.filter(project=self.request.tracer.project, report_date=report_date, product=product,shift=shift).exists()
		if exists:
			raise ValidationError('请勿重复提交该班次的良率报表')
	
		return report_date

class Yield_rateModelForm(BootStrapForm, forms.ModelForm):

	class Meta:
		model = models.Yield_rate
		fields = ['spi_1','pre_aoi_1','post_aoi_1','TOP_analysis1','cause_solution1','spi_2','pre_aoi_2','post_aoi_2','TOP_analysis2','cause_solution2','product','shift','report_date']

	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.request = request



class Output_by2hourModelForm(BootStrapForm, forms.ModelForm):

	class Meta:
		model = models.Output_by2hour
		fields = ['target1','achieve1','target2','achieve2','target3','achieve3','target4','achieve4','target5','achieve5','product','shift','report_date']

	def __init__(self, request, *args, **kwargs):
		super().__init__(*args, **kwargs)







